package com.ruochen.recipe.ui.fragment.raw;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.aries.ui.view.radius.RadiusRelativeLayout;
import com.lxj.xpopup.XPopup;
import com.ruochen.common.adapter.RecyclerCommonAdapter;
import com.ruochen.common.adapter.RecyclerViewHolder;
import com.ruochen.common.base.BaseMvpFragment;
import com.ruochen.common.listener.NoDoubleClickListener;
import com.ruochen.recipe.R;
import com.ruochen.recipe.customer.OrderSurePopup;
import com.ruochen.recipe.customer.RawMaterialItemDecoration;
import com.ruochen.recipe.customer.RawMaterialTypeItemDecoration;
import com.ruochen.recipe.databinding.FragmentRawMaterialBinding;
import com.ruochen.recipe.mvp.raw.RawMaterialCovenant;
import com.ruochen.recipe.mvp.raw.RawMaterialPresenter;

import java.util.Arrays;

/**
 * @Description 原材料
 * @Auth RuoChen
 * @Date 12/03/2021, 00:34
 **/
public class RawMaterialFragment extends BaseMvpFragment<FragmentRawMaterialBinding, RawMaterialPresenter> implements RawMaterialCovenant.MvpView {

    private int currentCategoryIndex;
    private RecyclerCommonAdapter<String> categoryAdapter;
    private RecyclerCommonAdapter<String> materialAdapter;
    private RecyclerCommonAdapter<String> selectedAdapter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        viewBinding.rvCategoryGrid.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false));
        viewBinding.rvCategoryGrid.addItemDecoration(new RawMaterialTypeItemDecoration());
        viewBinding.rvCategoryGrid.setAdapter(categoryAdapter = new RecyclerCommonAdapter<String>(mContext, R.layout.item_raw_material_type, Arrays.asList("1", "2", "2", "2", "2", "2", "2", "2",
                "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2")) {
            @Override
            protected void convert(RecyclerViewHolder holder, String item, int position) {
                RadiusRelativeLayout itemLayout = (RadiusRelativeLayout) holder.getConvertView();
                itemLayout.getDelegate().setSelected(currentCategoryIndex == position);
                itemLayout.getDelegate().init();
            }
        });
        categoryAdapter.setOnItemClickListener((view, holder, position) -> {
            if (currentCategoryIndex == position) {
                return;
            }
            currentCategoryIndex = position;
            categoryAdapter.notifyDataSetChanged();
        });

        viewBinding.rvIngredientsGrid.setLayoutManager(new GridLayoutManager(mContext, 4));
        viewBinding.rvIngredientsGrid.addItemDecoration(new RawMaterialItemDecoration());
        viewBinding.rvIngredientsGrid.setAdapter(materialAdapter = new RecyclerCommonAdapter<String>(mContext, R.layout.item_raw_material, Arrays.asList("1", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
                "2", "2", "2", "2", "2", "2", "2", "2", "2",
                "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2")) {
            @Override
            protected void convert(RecyclerViewHolder holder, String item, int position) {

            }
        });

        viewBinding.rvSelectedList.setLayoutManager(new LinearLayoutManager(mContext));
        viewBinding.rvSelectedList.setAdapter(selectedAdapter = new RecyclerCommonAdapter<String>(mContext, R.layout.item_raw_material_selected, Arrays.asList("1", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2",
                "2", "2", "2", "2", "2", "2", "2", "2", "2",
                "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2")) {
            @Override
            protected void convert(RecyclerViewHolder holder, String item, int position) {

            }
        });
        viewBinding.rtvSureOrder.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                new XPopup.Builder(mContext)
                        .dismissOnTouchOutside(false)
                        .dismissOnTouchOutside(false)
                        .enableDrag(false)
                        .autoOpenSoftInput(false)
                        .autoFocusEditText(false)
                        .moveUpToKeyboard(true)
                        .isDestroyOnDismiss(true)
                        .asCustom(new OrderSurePopup(mContext))
                        .show();
            }
        });

    }

    @Override
    protected RawMaterialPresenter createPresenter() {
        return new RawMaterialPresenter(this);
    }

}
